﻿using GalaSoft.MvvmLight.Ioc;
using MahApps.Metro.Controls;
using System;
using System.Diagnostics;
using System.Windows.Controls;
using System.Windows.Documents;


namespace AzureVMDashboard.UI
{

    public partial class MainWindow : MetroWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            //show settings UI if not well configured
            if (!App.Resources.IsWellConfigured)
                SimpleIoc.Default.GetInstance<MainViewModel>().CmdSettings.Execute(null);

            App.Resources.MainWindow = this;

        }

        private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
        {
            if (App.Resources.CurrentSubscription != null && !App.Resources.VMController.IsWorking)
            {
                //automatic refresh if the elapsed time is greater than 1 hour 
                var timeDifference = (DateTime.Now - App.Resources.CurrentSubscription.LastUpdate);
                if (timeDifference.Hours >= 1)
                    App.Resources.CurrentSubscription.RetrieveVMs();
            }
        }

        private void HandleRequestNavigate(object sender, System.Windows.Navigation.RequestNavigateEventArgs e)
        {
            string navigateUri = ((Hyperlink)sender).NavigateUri.ToString();
            Process.Start(new ProcessStartInfo(navigateUri));
            e.Handled = true;
        }    
    }
}
